package javbot.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javbot.Constants;

public class ScriptPanel extends JPanel {

	private static final long serialVersionUID = 2002051649880503327L;
	public JTextArea scriptInput = null;
	private PosPanel posPollPanel = null;
	private JScrollPane jScrollPane = null;

	/**
	 * This is the default constructor
	 */
	public ScriptPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);
		this.add(getPosPollPanel(), java.awt.BorderLayout.NORTH);
		this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
	}

	/**
	 * This method initializes scriptInput	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getScriptInput() {
		if (scriptInput == null) {
			scriptInput = new JTextArea();
			Font f = new Font("DialogInput", Font.PLAIN,12);
			scriptInput.setFont(f);
			scriptInput.setText("");
			scriptInput.setBackground(java.awt.Color.white);
		}
		return scriptInput;
	}

	/**
	 * This method initializes posPollPanel	
	 * 	
	 * @return jabot.gui.PosPollPanel	
	 */
	private PosPanel getPosPollPanel() {
		if (posPollPanel == null) {
			posPollPanel = new PosPanel(this);
		}
		return posPollPanel;
	}
	
	public int insertString(String s) {
		int pos = scriptInput.getCaretPosition();
		scriptInput.insert(s,pos);
		pos += s.length();
		((JComponent) scriptInput.getParent()).updateUI();
		scriptInput.setCaretPosition(pos);
		scriptInput.requestFocus();
		return pos;
	}
	
	public int insertPoint(Point point) {
		int pos = insertString("(" + point.x + "," + point.y + ")");
		scriptInput.setCaretPosition(pos);
		return pos;
		
	}	
	
	public void insertGoTo() {
		String s = "Go To (X,Y); ";
		int pos = insertString(s);
		scriptInput.select(pos-7, pos-2);
	}

	public void insertJumpTo() {
		String s = "Jump To (X,Y); ";
		int pos = insertString(s);
		scriptInput.select(pos-7, pos-2);
	}

	public void insertClick() {
		insertString("Click; ");
	}

	public void insertType() {
		String s = "Type ''; ";
		int pos = insertString(s);
		scriptInput.setCaretPosition(pos - 3);
	}


	public void insertWait() {
		String s = "Wait 1; ";
		int pos = insertString(s);
		scriptInput.select(pos-3, pos-2);
	}	
	
	public void insertExecute() {
		String s = "Execute ''; ";
		int pos = insertString(s);
		scriptInput.setCaretPosition(pos - 3);
	}	

	public void insertSample() {
		scriptInput.setText("");
		insertString(Constants.JAVBOT_SAMPLE_SCRIPT);
	}

	public void insertScreenshot() {
		insertString("Screenshot; ");
	}

	public void insertMailFiles() {
		String s = "Mail Files <email>; ";
		int pos = insertString(s);
		scriptInput.select(pos-9, pos-2);
		
	}
	

	public void insertKey(String s) {
		insertString(s + "; ");
		scriptInput.requestFocus();
		
	}	

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setPreferredSize(new Dimension(200,200));
			jScrollPane.setViewportView(getScriptInput());
		}
		return jScrollPane;
	}

	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		scriptInput.setEnabled(enabled);
		posPollPanel.setEnabled(enabled);
		jScrollPane.setEnabled(enabled);
	}
}
